package com.andy.mvp.poetry;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.AbsListView;
import android.widget.ListView;

import com.andy.app_core.net.databus.RxBus;
import com.andy.mvp.R;
import com.andy.mvp.poetry.adapter.PoetryAdapter;
import com.andy.mvp.poetry.bean.Proetry;
import com.andy.mvp.poetry.dagger.DaggerProetryCompontent;
import com.andy.mvp.poetry.dagger.ProetryPresenterModules;
import com.andy.mvp.poetry.presenter.ProetryPresenter;
import com.andy.mvp.poetry.view.IProetryView;

import java.util.ArrayList;

import javax.inject.Inject;

/**
 * @version V1.0
 * @描述:
 * @Company:
 * @auth: Created by AN .
 * @time: 2020/3/27 18:20
 * @des
 */
public class PoetryActivity extends AppCompatActivity implements IProetryView {
    ListView listView;

    @Inject
    ProetryPresenter proetryPresenter;
    int page =0;
    ArrayList<Proetry> mData=new ArrayList<Proetry>();
    PoetryAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poetry);
        listView = findViewById(R.id.listview);
        mAdapter=new PoetryAdapter(this,mData);
        listView.setAdapter(mAdapter);

        DaggerProetryCompontent.builder()
                .proetryPresenterModules(new ProetryPresenterModules(this))
                .build().inject(this);
        RxBus.getInstance().register(proetryPresenter);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState ==SCROLL_STATE_IDLE){
                    if(view.getLastVisiblePosition() == listView.getCount()-1){
                        proetryPresenter.loadMore(page++);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

    }

    @Override
    public void showProetrys(ArrayList<Proetry> proetryList) {
        mAdapter.setData(proetryList);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getInstance().unRegister(proetryPresenter);
    }

}
